package uk.co.beetec.android.npvrui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class NPvrUiActivity extends Activity {
	
    WebView webview = null;
    String npvruiServerAddress = null;
    String npvruiPort = null;
    String npvruiHtmlFile = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SharedPreferences prefs = getSharedPreferences("npvrui", MODE_PRIVATE);
        npvruiServerAddress = prefs.getString("npvruiServerAddress", "0.0.0.0");
        if ("0.0.0.0".equals(npvruiServerAddress)) {
        	Intent i = new Intent(this, NPvrUiSettingsActivity.class);
        	startActivity(i);
        	finish();
        }
        npvruiPort = prefs.getString("npvruiPort", "8866");
        npvruiHtmlFile = prefs.getString("npvruiHtmlFile", "and-client.html");
        
        final Activity activity = this;

        webview = (WebView)findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBuiltInZoomControls(false);
        webview.getSettings().setLightTouchEnabled(true);
        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setProgress(progress * 1000);
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }
        });

        webview.clearCache(true);
        webview.loadUrl("http://" + npvruiServerAddress + ":" + npvruiPort + "/touch/" + npvruiHtmlFile);
    }
}